package utils

import (
	"github.com/pkg/errors"
	"math/rand"
	"strings"
)

var (
	defaultLetters                 = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
	defaultActiveCodeLength        = 4
	defaultActiveCodeSegmentLength = 5
)

// GenerateRandomCode 生成会员兑换码，格式为WWWWW-XXXXX-YYYYY-ZZZZZ
func GenerateRandomCode() (string, error) {
	seed, err := GenerateUUID()
	if err != nil {
		return "", errors.Wrap(err, "生成UUID失败")
	}
	rand.Seed(seed)
	arr := make([]string, defaultActiveCodeLength)
	for i := 0; i < defaultActiveCodeLength; i++ {
		b := make([]rune, defaultActiveCodeSegmentLength)
		for j := range b {
			b[j] = defaultLetters[rand.Intn(len(defaultLetters))]
		}

		arr[i] = string(b)
	}
	return strings.Join(arr, "-"), nil
}
